/**
 * Created with JetBrains WebStorm.
 * User: Endre Balogh
 * Date: 5/29/13
 * Time: 4:35 PM
 * To change this template use File | Settings | File Templates.
 */

'use strict';

angular.module('ender.game.character', ['ender.game.services', 'module.ender.game.character.feats'])
    .value('attributeNames', {
        "strength" : "Strength",
        "dexterity" : "Dexterity",
        "constitution" : "Constitution",
        "intelligence" : "Intelligence",
        "wisdom" : "Wisdom",
        "charisma" : "Charisma"
    })
    .value('attributeDescriptions', {
        strength : "Strength measures a character's muscle and physical power. It determines effectiveness with melee weapons, thrown weapons and composite bows, and the total weight the character can carry before becoming encumbered.",
        dexterity : "Dexterity measures hand-eye coordination, agility, reflexes, and balance. It determines effectiveness with most ranged weapons, and it offers Dodge evasion rating and bonuses for Reflex saving throws, Initiative rolls, and a number of skills.",
        constitution : "Constitution represents a character's health and stamina. It determines the character's maximum health, and it offers a bonus for Fortitude saving throws and certain skills.",
        intelligence : "Intelligence determines how well a character learns and reasons. It determines the number of skill points gained when leveling up, and it offers a bonus to some skills. It also determines the number and effectiveness of spells for some caster characters.",
        wisdom : "Wisdom describes a character's willpower, common sense, perception, and intuition. It offers a bonus to Will saving throws and some skills. It also determines the number and effectiveness of spells for some caster characters.",
        charisma : "Charisma measures a character's force of personality, persuasiveness, personal magnetism, ability to lead, and physical attractiveness. It offers a bonus to some skills. It also determines the number and effectiveness of spells for some caster characters."
    })
    .value('skillNames', {
        "appraise": "Appraise",
        "bluff": "Bluff",
        "diplomacy": "Diplomacy"
    })
    .value('skillDescriptions', {
        "appraise": "Appraise allows characters to estimate the value of an item.",
        "bluff": "Bluff helps the character make the outrageous seem true. The skill encompasses fast-talking, conning, acting, and misdirection through words or body language.",
        "diplomacy": "Diplomacy involves etiquette, social grace, tact, subtlety, and a way with words. This skill represents the ability to give others the right impression of yourself, to negotiate effectively, and to influence others."
    })
    .value('saveNames', {
        "fortitude": "Fortitude",
        "reflex": "Reflex",
        "will": "Will"
    })
    .value('saveDescriptions', {
        will : "Will measures a character's ability to withstand damage thanks to his mental toughness. It reflects resistance to mental influence and dominate as well as many magical effects.",
        fortitude : "Fortitude measures a creature's ability to stand up to physical punishment or attacks against vitality and health, such as poison, paralysis and death magic.",
        reflex : "Reflex measures a character's ability to withstand damage thanks to his agility or quick reactions or to prevent snares."
    })
    .controller('CharacterController', function ($scope, gameStateService, attributeNames, attributeDescriptions, skillNames, skillDescriptions, saveNames, saveDescriptions, featHelper) {
        var character = $scope.character = gameStateService.getPlayerCharacter();

        function buildAttributeArray (character) {
            var array = [];
            for ( var attribute in character.attributes) {
                var bonus = character.getAttributeBonus(attribute);
                if (bonus > 0)
                    bonus = "+" + bonus;
                array.push({
                    name : attributeNames[attribute],
                    description : attributeDescriptions[attribute],
                    value : character.attributes[attribute],
                    bonus : bonus
                });
            }
            return array;
        }

        function buildSkillArray (character) {
            var array = [];
            for ( var skill in character.skills) {
                var bonus = character.getSkillBonus(skill);
                if (bonus > 0)
                    bonus = "+" + bonus;
                array.push({
                    name : skillNames[skill],
                    description : skillDescriptions[skill],
                    value : character.skills[skill],
                    bonus : bonus
                });
            }
            return array;
        }

        function buildSaveBonusArray(character) {
            if (!character.bonus)
                return [];

            var array = [];

            var saveArray = [ 'fortitude', 'reflex', 'will' ];
            for (var i=0; i<saveArray.length; i++){
                var save = saveArray[i];
                array.push({
                    name : saveNames[save],
                    description : saveDescriptions[save],
                    value : character.bonus.save[save] + character.bonus.save.all
                });
            }
            return array;
        }



        $scope.attributeArray = buildAttributeArray(character);
        $scope.skillArray = buildSkillArray(character);
        $scope.saveBonusArray = buildSaveBonusArray(character);
        $scope.$watch('character.feats.length', function(){
            $scope.featCategoryArray = featHelper.buildFeatCategoryArray(character.feats);
        });
    })
    .directive('characterInventory', function() {
        return {
            restrict : 'E',
            replace : true,
            scope : {
                inventory : '='
            },
            templateUrl : 'templates/inventory.html',
            link : function(scope, element, attrs) {
                scope.$watch('inventory', function() {
                    for ( var slot in scope.inventory) {
                        var item = scope.inventory[slot];
                        var slotDiv = element.find('div.inventory-slot.' + slot);
                        slotDiv.html('<img src="images/icons/' + item.icon + '" />');

//                        if (item instanceof Ender.Game.Classes.Weapon) {
//                            var description = item.name + "\n" + "Damage: " + item.getDamageString() + "\n" + "Critical range: " + item.getCritRangeString();
//                            slotDiv.attr('title', description);
//                        }
//                        if (item instanceof Ender.Game.Classes.Shield) {
//                            var description = item.name + "\n";
//                            slotDiv.attr('title', description);
//                        }
//                        if (item instanceof Ender.Game.Classes.Armor) {
//                            var description = item.name + "\n";
//                            slotDiv.attr('title', description);
//                        }
//                        if (item instanceof Ender.Game.Classes.Accessory) {
//                            var description = item.name + "\n";
//                            slotDiv.attr('title', description);
//                        }
                    }
                }, true);
            }
        };
    });